package com.hqyj.server.json;

//封装一个返回json格式数据的类型
//干嘛呢？所有的Controller都默认返回R对象。
//R对象里面有啥。
//request
//服务器老人家，返回的什么东西。response

//response

import lombok.Data;

import java.io.Serializable;

@Data
public class R implements Serializable, Cloneable {

    private Object data; //返回的数据
    private String msg; //返回的消息
    private Integer code; //状态码


    public static final int SUCCESS = 200;
    public static final int FAIL = 400; //表示失败
    public static final int ERROR = 500; //错误，服务器出现了异常。

    //表示成功
    public static R success() {
        R r = new R();
        r.code = SUCCESS;
        r.msg = "success";
        return r;
    }

    public static R success(String msg) {
        R r = new R();
        r.code = SUCCESS;
        r.msg = msg;
        return r;
    }

    public static R success(String msg, Object data) {
        R r = new R();
        r.code = SUCCESS;
        r.msg = msg;
        r.data = data;
        return r;
    }



    public static R fail() {
        R r = new R();
        r.code = FAIL;
        r.msg = "failure";
        return r;
    }

    public static R fail(String msg) {
        R r = new R();
        r.code = FAIL;
        r.msg = msg;
        return r;
    }

    public static R fail(String msg, Object data) {
        R r = new R();
        r.code = FAIL;
        r.msg = "failure";
        r.data = data;
        return r;
    }

    public static R error() {
        R r = new R();
        r.code = ERROR;
        r.msg = "error";
        return r;
    }

    public static R error(String msg) {
        R r = new R();
        r.code = ERROR;
        r.msg = msg;
        return r;
    }
}
